package utils;

import java.util.HashMap;
import java.util.Vector;

import datastructures.Pair;

public class ArgMin {

	public static Pair<String,Double> argmin(HashMap<String, Double> elements)
	{
		Pair<String,Double> min = null;
		for (String key : elements.keySet())
		{
			if (min == null) min = new Pair<String,Double>(key,elements.get(key));
			if (min.second > elements.get(key).doubleValue())
				min = new Pair<String,Double>(key,elements.get(key));
		}
		return min;
	}
	
	// returns, in a vector format of top n arguments, or top k if only k elements in it.
	// pretty slow version of the code.
	public static Vector<Pair<String,Double>> topArgmin(HashMap<String, Double> elements, int n)
	{
		Vector<Pair<String,Double>> returner = new Vector<Pair<String,Double>>();
		HashMap<String, Double> elementCopy = getCopy(elements);
		for (int i = 0; i < n; i++)
		{
			Pair<String,Double> curr = argmin(elementCopy);
			if (curr == null) return returner;
			elementCopy.remove(curr.first);
			returner.add(curr);
		}
		return returner;
	}

	private static HashMap<String, Double> getCopy(
			HashMap<String, Double> elements) {
		HashMap<String, Double> returner = new HashMap<String, Double>();
		for (String key : elements.keySet())
		{
			returner.put(key, elements.get(key));
		}
		return returner;
	}
}

